package com.example.mutildatasource.service.impl;

import com.example.mutildatasource.entity.Clazz;
import com.example.mutildatasource.entity.Student;
import com.example.mutildatasource.entity.TreeNode;
import com.example.mutildatasource.service.ClazzService;
import com.example.mutildatasource.service.StudentService;
import com.example.mutildatasource.service.ZtreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class ZtreeServiceImpl implements ZtreeService {

    @Autowired
    private StudentService studentService;
    @Autowired
    private ClazzService clazzService;

    public List< ? super TreeNode> buildTree(){
        List< ? super TreeNode> treeNodeList  = new ArrayList();
        try {
            TreeNode treeNode = new TreeNode();
            treeNode.setTreePid("0");
            treeNode.setTreeId("treeNode1");
            treeNode.setTreeNodeName("软件学院");
            treeNodeList.add(treeNode);
            List<Clazz> clazzList = clazzService.getAllByCondition();
            if(clazzList.size()>0){
                for (Clazz clazz:clazzList){
                    clazz.setTreePid("treeNode1");
                    clazz.setTreeId("treeNode2-"+clazz.getId());
                    clazz.setTreeNodeName(clazz.getFullname());
                    treeNodeList.add(clazz);//加入二级节点菜单
                    Student condition  = new Student();
                    condition.setClassId(new BigDecimal(clazz.getId()));
                    List<Student> studentList = this.studentService.queryByCondition(condition);
                    if(studentList.size()>0){
                        for (Student student : studentList) {
                            student.setTreePid(clazz.getTreeId());
                            student.setTreeId("treeNode3-"+student.getId());
                            student.setTreeNodeName(student.getName()+"-"+student.getSex()+"-"+student.getPhone());
                            treeNodeList.add(student);//加入三级节点
                        }
                    }

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return treeNodeList;
    }

    public List< ? super TreeNode> asynchBuildTree(String treeId,String isParent){
        List< ? super TreeNode> treeNodeList  = new ArrayList();
        try {
            if(treeId == null || "".equals(treeId)){
                TreeNode treeNode = new TreeNode();
                treeNode.setTreePid("0");
                treeNode.setTreeId("treeNode1");
                treeNode.setTreeNodeName("软件学院");
                treeNode.setIsParent(true);
                treeNodeList.add(treeNode);
                List<Clazz> clazzList = clazzService.getAllByCondition();
                if(clazzList.size()>0){
                    for (Clazz clazz:clazzList){
                        clazz.setTreePid("treeNode1");
                        clazz.setTreeId("treeNode2-"+clazz.getId());
                        clazz.setTreeNodeName(clazz.getFullname());
                        clazz.setIsParent(true);
                        treeNodeList.add(clazz);//加入二级节点菜单
                    }
                }
            }else {
                if(isParent.equals("true")){
                    //如果是父菜单就加载下一级的数据，一般是去表中查找pid为该节点treeId的记录
                    //这里没有存放节点关系的数据表，只能根据class_id查找班级下面的学生
                    String class_id = treeId.substring(10);
                    Student condition  = new Student();
                    condition.setClassId(new BigDecimal(class_id));
                    List<Student> studentList = this.studentService.queryByCondition(condition);
                    if(studentList.size()>0){
                        for (Student student : studentList) {
                            student.setTreePid(treeId);
                            student.setTreeId("treeNode3-"+student.getId());
                            student.setTreeNodeName(student.getName()+"-"+student.getSex()+"-"+student.getPhone());
                            treeNodeList.add(student);//加入三级节点
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return treeNodeList;
    }

}
